package cn.edu.szpt.qqdemov3.dialogs;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import java.util.ArrayList;
import java.util.List;

import cn.edu.szpt.qqdemov3.MainActivity;
import cn.edu.szpt.qqdemov3.R;
import cn.edu.szpt.qqdemov3.dbutils.MyDbHelper;

public class NewContactDialog extends DialogFragment {
    private Spinner dlg_spinContact;
    private Button dlg_btnOK;
    private Button dlg_btnCancel;
    private OnDialogCompletedListener listener;

    public void setListener(OnDialogCompletedListener listener) {
        this.listener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=null;
        view = inflater.inflate(R.layout.dialog_newcontact,container,false);
        dlg_spinContact = view.findViewById(R.id.dlg_spinContact);
        dlg_btnOK=view.findViewById(R.id.dlg_btnOK);
        dlg_btnCancel = view.findViewById(R.id.dlg_btnCancel);

        dlg_btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),R.layout.support_simple_spinner_dropdown_item,getFriendsList());

        dlg_spinContact.setAdapter(adapter);

        dlg_btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDbHelper helper=new MyDbHelper(getContext());
                SQLiteDatabase db = helper.getWritableDatabase();
                String sql ="insert into QQ_Contact(qq_num,belong_qq) values(?,?) ";
                if(dlg_spinContact.getSelectedItemPosition()>-1) {
                    String s = dlg_spinContact.getSelectedItem().toString();
                    String qqnum = s.split("\t\t")[0];
                    db.execSQL(sql, new Object[]{qqnum, MainActivity.loginedUser.getQq_num()});
                    listener.onCompleted();
                }
                dismiss();
            }
        });
        return view;
    }

    @SuppressLint("Range")
    private List<String> getFriendsList(){
        List<String> list = new ArrayList<>();
        MyDbHelper helper = new MyDbHelper(getContext());
        SQLiteDatabase db = helper.getReadableDatabase();

        String sql ="select * from QQ_Login where qq_num not in" +
                " (select qq_num from QQ_Contact where belong_qq=?) and qq_num<>?";
        Cursor cursor = db.rawQuery(sql,new String[]{MainActivity.loginedUser.getQq_num(),MainActivity.loginedUser.getQq_num()});

        while(cursor.moveToNext()){
            list.add(cursor.getString(cursor.getColumnIndex("qq_num")) + "\t\t" +
                    cursor.getString(cursor.getColumnIndex("qq_name")));
        }

        return list;
    }
}
